/*
 * Copyright (C) 2009 Georgios J. Georgopoulos, All rights reserved.
 * 
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * 
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
 * site: http://www.fsf.org.
 */
package org.gwt.mosaic.xul.client.ui;

import org.gwt.mosaic.ui.client.HTMLLabel;
import org.gwt.mosaic.ui.client.util.WidgetHelper;

import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.widgetideas.client.ResizableWidget;
import com.google.gwt.widgetideas.client.ResizableWidgetCollection;

/**
 * This element is used to create a block of text. The text can be set either
 * with the {@code value} attribute or by placing text inside the open and close
 * description tags. The {@code value} attribute is used to set text that
 * appears in a single line. If text appears as a child of the description, it
 * will wrap to multiple lines. It may contain arbitrary markup, which can be
 * styled as needed.
 * 
 * @author georgopoulos.georgios(at)gmail.com
 * 
 */
public class Description extends Element {
  private static final long serialVersionUID = -7373021490587918971L;

  private transient HTMLLabel html = null;
  private transient ResizableWidget resizableWidget = null;

  // -----------------------------------------------------------------------
  /**
   * The text to be used for the label.
   */
  public static final String VALUE = "value";

  public String getValue() {
    return getString(VALUE);
  }

  public void setValue(String value) {
    putString(VALUE, value);
  }

  public void setValue(String value, boolean wordWrap) {
    setValue(value);
    this.wordWrap = wordWrap;
  }

  // -----------------------------------------------------------------------
  private boolean wordWrap = false;

  // -----------------------------------------------------------------------
  public Description() {
    super();
  }

  // -----------------------------------------------------------------------
  @Override
  protected Widget createUI() {
    if (html == null) {
      html = new HTMLLabel();
    }

    syncUI(html);

    return html;
  }

  @Override
  protected void syncUI(Widget ui) {
    super.syncUI(ui);

    String value = getValue();
    if (value != null) {
      html.setHTML(value);
    }

    html.setWordWrap(wordWrap);

    // XXX monitor word wraps (it must be a better way to do that)
    if (wordWrap) {
      ResizableWidgetCollection.get().add(
          resizableWidget = new ResizableWidget() {
            public com.google.gwt.user.client.Element getElement() {
              return html.getElement();
            }

            public boolean isAttached() {
              return html.isAttached();
            }

            public void onResize(int width, int height) {
              WidgetHelper.invalidate(html);
              html.setSize("auto", "auto");
            }
          });
    } else if (resizableWidget != null) {
      ResizableWidgetCollection.get().remove(resizableWidget);
      resizableWidget = null;
    }
  }

}
